﻿using Hotel.Model.Store;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class StoreDAL
    {
        MyDBContext db;
        public StoreDAL(MyDBContext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 增加门店
        /// </summary>
        /// <param name="info">门店的实体类</param>
        /// <returns>返回影响的行数</returns>
        public int PostAddStore(T_Store info)
        {
            try
            {
                    db.T_Store.Add(info);
                    return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据门店名称查询是否存在
        /// </summary>
        /// <param name="storeName">门店名称</param>
        /// <returns>返回符合条件的数据</returns>
        public int SearchStoreName(string storeName)
        {
            try
            {
                int res = db.T_Store.Where(c => c.StoreName == storeName).Count();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取门店列表
        /// </summary>
        /// <returns></returns>
        public List<T_Store> GetStoreList()
        {
            try
            {
                List<T_Store> list = db.T_Store.Where(c=>c.IsDelete == false).ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 删除门店
        /// </summary>
        /// <param name="id">门店的id</param>
        /// <returns>返回影响的行数</returns>
        public int GetDelStoreItem(int id)
        {
            try
            {
                //先根据id找到这一项（对象）
                var info = db.T_Store.Find(id);
                //修改逻辑删除的属性
                info.IsDelete = true;
                return db.SaveChanges();

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取所有门店的销售信息
        /// </summary>
        /// <returns>返回符合条件的数据</returns>
        public IQueryable GetStoreCountList(int? id = null)
        {
            try
            {
                //获取所有的门店消费信息
                var list = db.T_Store.Join(db.T_Store_Member, a => a.StoreId, b => b.StoreId, (a, b) => new
                {
                    a.StoreId,
                    a.StoreName,
                    b.M_Money,
                    b.M_Pay_Money,
                    b.M_Date
                });

                if(id != null)
                {
                    list = list.Where(c => c.StoreId == id);
                }

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
